package Action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.userdao;
import Entity.User;
import mapper.Usermapper;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int sum=0;
		String search= request.getParameter("search");
		int page = Integer.parseInt(request.getParameter("cpage"));
		if(search==null){
			Usermapper um =new userdao();
			sum=um.SearchStudent().size();
			if(sum%10==0){
				sum=sum/10;
			}
			else
			{
				sum=sum/10+1;
			}
			List<User> users= um.SearchStudentBypage(page);
			System.out.println(sum);
			request.setAttribute("users", users);
			request.setAttribute("sum", sum);
			request.setAttribute("cpage", page);
		}
		else{
		Usermapper u = new userdao();
		sum=u.SearchUser(search).size();
		if(sum%10==0){
			sum=sum/10;
		}
		else
		{
			sum=sum/10+1;
		}
		List<User> users =u.SearchUserBypage(search,page);
		System.out.println(search);
		request.setAttribute("users", users);
		request.setAttribute("cpage", page);
		request.setAttribute("sum", sum);
		request.setAttribute("search", search);
		}
		request.getRequestDispatcher("search_userInfor.jsp").forward(request,response);
		
		
	}

}
